package br.com.sisnema.ws.exemplo;

import java.io.IOException;
import java.io.InputStream;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class WSActivity extends Activity implements Runnable{
    
	private AndroidHttpTransport transporte;
	private TextView textView1;
	
	private String namespace = "http://www.oorsprong.org/websamples.countryinfo";
	private String METHOD_NAME = "CountryFlag";
	private String URL = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso";
	private String SOAP_ACTION = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?op=CountryFlag";
    
	private SoapObject soap;
	private SoapSerializationEnvelope envelope;
	private ProgressDialog progress;
	private SoapPrimitive resposta;
	private Drawable drawable;
	private ImageView imgView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imgView = (ImageView)findViewById(R.id.imageView1);
        Button btnOK = (Button)findViewById(R.id.btnOK);
        final EditText txtNome = (EditText)findViewById(R.id.txtNome);
        textView1 = (TextView)findViewById(R.id.textView1);
        
        btnOK.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
		        soap = new SoapObject(namespace, METHOD_NAME);
		        
		        
		        PropertyInfo p1 = new PropertyInfo();
		        p1.setName("sCountryISOCode");
		        p1.setValue(txtNome.getText().toString());
		        p1.setType(String.class);
		        
		        soap.addProperty(p1);
		        
		        
		        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		        
		        //envelope.dotNet = true; 
		        
		        envelope.setOutputSoapObject(soap);
		        
		        transporte = new AndroidHttpTransport(URL);
		        
		   
		        progress = ProgressDialog.show(WSActivity.this, "Carregando", "Aguarde...", true, false);
				Thread th = new Thread(WSActivity.this);
				th.start();

			}
		});
    }

	@Override
	public void run() {
		try{
			transporte.call(SOAP_ACTION, envelope);
			
			resposta = (SoapPrimitive)envelope.getResponse();
			
			drawable = loadIMG(resposta.toString()); 
			handler.sendEmptyMessage(0);
		}catch (Exception ex){
			Log.d("Erro = ", ex.getMessage());
		}
	}
	
	private Drawable loadIMG(String url) {     
		try     {         
			InputStream is = (InputStream) new java.net.URL(url).getContent();         
			Drawable d = Drawable.createFromStream(is, "src name");         
			return d;     
		}catch (Exception e){          
				return null;     
		} 
	} 
	
	private Handler handler = new Handler(){
		@Override
		 public void handleMessage(Message msg) {
			 progress.dismiss();
			 //textView1.setText(resposta.toString());
			 imgView.setImageDrawable(drawable); 
		 	}
		 };
}